package com.spoto.oa.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.spoto.oa.model.Employee;
import com.spoto.oa.service.EmployeeServ;
import com.spoto.oa.service.impl.EmployeeServImpl;

public class ContactServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ContactServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type=request.getParameter("op_type");
		System.out.println(type);
		Employee employee = new Employee();
		EmployeeServ employeeServ = new EmployeeServImpl();
		if ("to_list".equals(type)) {
			List<Employee> employees = null;
			employees = employeeServ.getAllEmployees();
			request.setAttribute("employees", employees);
			request.getRequestDispatcher("/contact/list_employee.jsp")
					.forward(request, response);
		}else if ("to_listDetail".equals(type)) { 
			String username = request.getParameter("username");
			List<Employee> employees = employeeServ.searchByUserName(username);
			request.setCharacterEncoding("gbk");
			request.setAttribute("employee", employees.get(0));
				request.getRequestDispatcher("/contact/listDetail_employee.jsp")
				.forward(request, response);
		}else if ("search".equals(type)) {
			List<Employee> employees = null;
			String searchType = request.getParameter("searchType");
			String infor = request.getParameter("infor");
			if ("员工帐号".equals(searchType)) {
				employees = employeeServ.searchByUserName(infor);

			} else if ("员工姓名".equals(searchType)) {
				employees = employeeServ.searchByName(infor);
			}else if ("QQ".equals(searchType)) {
				employees = employeeServ.searchByQq(infor);
			}else if ("手机".equals(searchType)) {
				employees = employeeServ.searchByCell(infor);
			}
			request.setAttribute("employees", employees);
			request.setAttribute("infor", infor);
			request.setAttribute("searchType", searchType);
			request.getRequestDispatcher("/contact/list_employee.jsp?op_type=to_list")
					.forward(request, response);
		}

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
